package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.statistics.StatisticsLastThreeMonthsFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.statistics.StatisticLastThreeMonthsDto;
import com.info.zhiduoduo.repository.mapper.company.CompanyProjectMapper;
import com.info.zhiduoduo.repository.mapper.crowdsource.CrowdsourceProjectMapper;
import com.info.zhiduoduo.repository.mapper.taxer.TaxerInvoiceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Trink丶
 */
@Service
@Slf4j
public class StatisticsService extends BaseService {


    @Autowired
    CompanyProjectMapper companyProjectMapper;

    @Autowired
    CrowdsourceProjectMapper crowdsourceProjectMapper;

    @Autowired
    TaxerInvoiceMapper taxerInvoiceMapper;


    public SmartResult compInfoOfCompanyLastThreeMonths(BaseSimpleFormBean<StatisticsLastThreeMonthsFormBean> formBean) {
        String name = formBean.getReqdata().getName();
        Page<StatisticLastThreeMonthsDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<StatisticLastThreeMonthsDto> result = companyProjectMapper.getCompanyStatisticLastNMonths(name, 3);
        result.forEach(statisticLastThreeMonthsDto -> {
            BigDecimal gmv = statisticLastThreeMonthsDto.getGmv();
            statisticLastThreeMonthsDto.setGmv(gmv.divide(new BigDecimal("3"),2,BigDecimal.ROUND_HALF_UP));
        });
        return SmartResult.success().setList(result).setTotalrecords(page.getTotal()).setTotalpages(page.getPages()).setCurrpage(page.getPageNum());
    }

    public SmartResult compInfoOfCrowdsourceLastThreeMonths(BaseSimpleFormBean<StatisticsLastThreeMonthsFormBean> formBean) {
        String name = formBean.getReqdata().getName();
        Page<StatisticLastThreeMonthsDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<StatisticLastThreeMonthsDto> result = crowdsourceProjectMapper.getCrowdsourceStatisticLastNMonths(name, 3);
        result.forEach(statisticLastThreeMonthsDto -> {
            BigDecimal gmv = statisticLastThreeMonthsDto.getGmv();
            statisticLastThreeMonthsDto.setGmv(gmv.divide(new BigDecimal("3"),2,BigDecimal.ROUND_HALF_UP));
        });
        return SmartResult.success().setList(result).setTotalrecords(page.getTotal()).setTotalpages(page.getPages()).setCurrpage(page.getPageNum());
    }

    public SmartResult infoOfTaxerLastThreeMonths(BaseSimpleFormBean<StatisticsLastThreeMonthsFormBean> formBean) {
        String name = formBean.getReqdata().getName();
        Page<StatisticLastThreeMonthsDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<StatisticLastThreeMonthsDto> result = taxerInvoiceMapper.getTaxerStatisticLastNMonths(name, 3);
        result.forEach(statisticLastThreeMonthsDto -> {
            BigDecimal gmv = statisticLastThreeMonthsDto.getGmv();
            statisticLastThreeMonthsDto.setGmv(gmv.divide(new BigDecimal("3"),2,BigDecimal.ROUND_HALF_UP));
        });
        return SmartResult.success().setList(result).setTotalrecords(page.getTotal()).setTotalpages(page.getPages()).setCurrpage(page.getPageNum());
    }
}
